package com.zhou.mongo.controller;

import com.zhou.mongo.dao.StudentDao;
import com.zhou.mongo.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;

@RestController
public class StudentContoller {
    @Autowired
    private StudentDao studentDao;

    @GetMapping("find")
    public String find(){
        Student user=new Student();
        user.setId(11);
        user.setName("小明");
        user.setAge(20);
        user.setSex("男");
        user.setLevel("六班");

        studentDao.save(user);
        Student user1= studentDao.findUserByName("小明");
        System.out.println("user is "+user1);
       return "ok";
    }
    @GetMapping("saveList")
    public String saveList(){
        for(int i=0;i<100;i++){
            Student user=new Student();
            user.setId(i);
            user.setName("小明"+i);
            user.setAge(new Random().nextInt(60)+15);
            user.setSex("男");
            user.setLevel("六班");
            studentDao.save(user);
        }
        return "result:";
    }
    @GetMapping("findByPage")
    public String findByPage(Integer pageNo,Integer pageSize){
        List<Student> list=studentDao.findByPage(pageNo,pageSize);
        int  lp=list.size();
        for(int i=0;i<lp;i++){
            if (i>=10) break;
            System.out.println(list.get(i).toString());
        }
        return "result:"+list.size();
    }
}
